/**
******************************************************************************
* @file           : task_alog_c.cpp
* @author         : wyj
* @brief          : C语言语法测试
* @attention      : None
* @date           : 2025/5/9
******************************************************************************
*/

#ifdef __cplusplus
extern "C" {
#endif

#include "geography/studio_proj_c.h"

#ifdef __cplusplus
}
#endif

#include <stdio.h>
#include <stdio.h>
#include "geometry/studio_geo_utils.h"
#include "geometry/studio_geo_algo.h"

int main() {
    printf("\n\n===================== %s =====================\n\n", __FILE__);
    silly::geo::utils::init_gdal_env();
    std::string path;
#ifdef IS_WINDOWS
    path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
#else
    path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
#endif

    std::cout << "path: " << path << std::endl;
    std::vector<studio_geo_coll> res_collections;
    std::vector<studio_geo_coll> collections;
    silly::geo::utils::read_geo_coll(path, collections);
    for (auto &coll: collections) {
        // ------------- 转换为高斯投影 -------------
        studio_line gauss_line;
        double central = static_cast<int>(coll.m_line[0].x / 3) * 3;
        for (auto &point: coll.m_line) {
            double gx = 0.0;
            double gy = 0.0;
            lonlat_to_gauss(central, point.x, point.y, &gx, &gy);
            gauss_line.push_back(studio_point(gx, gy));
        }

        // 简化线段，目标点数为28个
        int max_points = 28;
        double epsilon = 0.001;

        // studio_line gs_simplified_line = simplify_line_2(gauss_line, max_points);
        studio_line gs_simplified_line;
        bool res = geo_vacuate::vacuate(gauss_line, max_points, epsilon, gs_simplified_line);
        if (!res) {
            std::cout << "Failed to simplify line." << std::endl;
            return 1;
        }
        studio_line simplified_line;
        // 高斯投影在转回经纬度
        for (auto &point: gs_simplified_line) {
            double lon = 0.0;
            double lat = 0.0;
            gauss_to_lonlat(central, point.x, point.y, &lon, &lat);
            simplified_line.push_back(studio_point(lon, lat));
        }

        studio_geo_coll temp;
        temp.m_type = enum_geometry_type::egtLineString;
        temp.m_line = simplified_line;
        res_collections.push_back(temp);
        break;
    }
    std::string output_path;
#ifdef IS_WINDOWS
    output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
#else
    output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples_res_1_c.geojson";
#endif
    silly::geo::utils::write_geo_coll(output_path, res_collections);

    silly::geo::utils::destroy_gdal_env();

    return 0;
}
